//
//  LockView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/20.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "LockView.h"
#import "LockCell.h"
@interface LockView ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_list;
    NSMutableArray *_data;
    UIView *whiteView;
    UILabel *countLab;
}
@end
@implementation LockView
//创建实例的方法
- (instancetype)initWithArr:(NSMutableArray *)data Count:(NSString *)count
{
    if (self = [super init]) {
        _data = [NSMutableArray array];
        self.frame = CGRectMake(0, 0, WYMWITH, WYMHEIGHT);
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        whiteView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, WYMWITH - 40, WYMHEIGHT - 250*app.autoSizeScaleY)];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
        //分割线
        [whiteView addSubview:[WYPublic getFenGexian:0 :80 :whiteView.frame.size.width :0.5]];
        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 4, whiteView.frame.size.width-20, 22)];
        titleLab.numberOfLines = 2;
        
        titleLab.font = [UIFont systemFontOfSize:14];
        titleLab.text = @"正在支付中订单信息";
        [whiteView addSubview:titleLab];
        
        countLab = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLab.frame), whiteView.frame.size.width - 8, 16)];
        countLab.font = [UIFont systemFontOfSize:14];
        
        countLab.textColor = QBB_RED_COLOR;
        [whiteView addSubview:countLab];
        //若支付交易失败，购买次数将进行返还，导致购买进度降低
        UILabel *tiplab = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(countLab.frame), whiteView.frame.size.width - 8, 37)];
        tiplab.font = [UIFont systemFontOfSize:14];
        [tiplab setTextColor:[UIColor darkGrayColor]];
        [tiplab setText:@"若支付交易失败，购买次数将进行返还，导致购买进度降低"];
        [tiplab setNumberOfLines:2];
        [whiteView addSubview:tiplab];

        //分割线
        [whiteView addSubview:[WYPublic getFenGexian:0 :whiteView.frame.size.height - 50 :whiteView.frame.size.width :0.5]];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        backBtn.frame = CGRectMake(0, 0, 200, 50);
        backBtn.center = CGPointMake(whiteView.frame.size.width/2, whiteView.frame.size.height - 25);
        [backBtn setTitle:@"确定" forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(backBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [whiteView addSubview:backBtn];
        [self loadTableView];
        [self addSubview:whiteView];

        if (!data) {
            //
            [WYPublic addHudToWindow];
            NSDictionary *params = @{@"id":count};
            [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,@"api/ApiGoods/getLockedList"] Parameters:params successBlock:^(NSDictionary *dic) {
                countLab.text = [NSString stringWithFormat:@"累计次数:%@次",dic[@"data"][@"locked_total"]];
                _list = dic[@"data"][@"locked_list"];
                if (_list.count > 0) {
                    for (NSDictionary *lock in _list) {
                        LockModel *model = [[LockModel alloc]init];
                        model.lock_code = lock[@"code"];
                        model.lock_gonum = lock[@"gonumber"];
                        model.lock_time = lock[@"time"];
                        model.lock_uid = lock[@"uid"];
                        [_data addObject:model];
                    }
                }
                [_tableView reloadData];

                [WYPublic removeHudToWindow];
            } failedBlock:^{
                [WYPublic removeHudToWindow];
            } needLog:NO];

        }
        else{
            countLab.text = [NSString stringWithFormat:@"累计次数:%@次",count];
            _list = data;
            //解析数据
            if (_list.count > 0) {
                for (NSDictionary *lock in _list) {
                    LockModel *model = [[LockModel alloc]init];
                    model.lock_code = lock[@"code"];
                    model.lock_gonum = lock[@"gonumber"];
                    model.lock_time = lock[@"time"];
                    model.lock_uid = lock[@"uid"];
                    [_data addObject:model];
                }
            }
            [_tableView reloadData];
        }
        
    }
    return self;
}
//确定按钮的监听事件
- (void)backBtnOnClick
{
    [self removeFromSuperview];
}


//加载表格
- (void)loadTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 80.5, whiteView.frame.size.width, whiteView.frame.size.height - 131)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"LockCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 75.0;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [whiteView addSubview:_tableView];
}
#pragma mark -- 表格协议函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LockCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count > 0) {
        LockModel *model = _data[indexPath.row];
        [cell fillCellWithModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


@end
